package controller;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;




import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import service.LoginService;
import vo.Address;
import vo.Store;
@Controller
@SessionAttributes(value = "loginUser")
public class LoginController {
	@Autowired(required=true)
	LoginService ls;
	@RequestMapping("/login")
	public String login(String sailName,String sailPasswd,Model model){
		System.out.println(sailName+":"+sailPasswd);
		Store sp=null;
		sp=ls.login(sailName,sailPasswd);
		//JSONObject jobject=JSONObject.fromObject(sp);
		
		if(sp!=null){
			model.addAttribute("loginUser",sp);
			return "redirect:/storeIndex.jsp";		
		}
		return "redirect:/login.jsp"; 
			
	
	}
	@RequestMapping("/register") 
	
	public String regist(Store sp,Address address,HttpServletRequest request){
		
		int flag=0;
		sp.setAddress(address);
		System.out.println(sp.getAddress().getCity());
	if(sp!=null){
			flag=ls.regist(sp);
			
		}
		if(flag!=0){
			return "login";
		}	
		return "regist";
	}
	                                                                                                                                                                                                                                                                                                                       
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
